import { UseFilters } from '@nestjs/common';
import { Args, Int, Mutation, Resolver } from '@nestjs/graphql';
import { GrpcFilter } from 'src/common/grpc/grpc.filter';
import { Question } from './question.entity';
import { GrpcService } from 'src/common/grpc/grpc.service';
import { User } from 'src/user/user.decorator';
import { UserEntity } from 'src/user/user.entity';

@UseFilters(GrpcFilter)
@Resolver(() => Question)
export class QuestionResolver {
  constructor(private readonly grpcService: GrpcService) {}

  @Mutation(() => Question)
  async createQuestion(
    @User() user: UserEntity,
    @Args('title') title: string,
    @Args('problemId') problemId: number,
    @Args('categoryId', { type: () => Int }) categoryId: number,
    @Args('content') content: string,
    @Args('verifyCode') verifyCode: string,
  ): Promise<Question> {
    const pbp = await this.grpcService.service
      .createQuestion({ ID: 0, userId: user._id, problemId, title, content, answers: [] })
      .toPromise();
    const p: Question = { _id: pbp.ID, ...pbp };
    return p;
  }

  @Mutation(() => Boolean)
  async removeQuestion(@Args('_id') _id: number): Promise<boolean> {
    this.grpcService.service.removeQuestion({ ID: _id });
    return true;
  }
}
